package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.v4.content.ContextCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;
import com.tsugun.mediaplayer.presenter.service.MusicAudioService;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.activity.BaseActivity;

import java.util.List;

import static com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper.*;

/**
 * 列表适配器
 * Created by shize on 2017/3/30.
 */

public class MusicItemAdapter extends RecyclerView.Adapter<MusicItemAdapter.RecyclerViewHolder> {

    private static int COLOR_LIGHT; // select图片中字体颜色
    private static int COLOR_NORMAL;
    private LayoutInflater mInflater;
    private List<MusicModel> mData;
    private OnMusicItemClickListener mOnMusicItemClickListener;
    private ColorStateList mThemeColorStateList;// 当前主题色
    private int mCurrent = -1; // 当前播放的位置
    private ListProvider mProvider;

    public interface OnMusicItemClickListener {
        void onItemClick(int position);

        void onMenuClick(int position);
    }

    public MusicItemAdapter(Context mContext, List<MusicModel> mData) {
        this.mData = mData;
        COLOR_LIGHT = ContextCompat.getColor(mContext, R.color.grey_50);
        COLOR_NORMAL = ContextCompat.getColor(mContext, R.color.grey_600);
        mInflater = LayoutInflater.from(mContext);
        mThemeColorStateList = new ColorStateList(new int[][]{new int[0]}, new int[]{
                SettingPreferenceHelper.INSTANCE.getThemeColorPreference(mContext)});
        mProvider = ((BaseActivity) mContext).getListProvider();
        currentPositionOnList();
    }

    /**
     * 当前位置
     */
    private void currentPositionOnList() {
        if (MusicAudioService.Companion.getMState() != PlaybackStateCompat.STATE_STOPPED) {
            List<MusicModel> musicModelList = (List<MusicModel>) mProvider.getPlayList();
            MusicModel musicModel = musicModelList.get(MusicAudioService.Companion.getMCurrentPosition());
            if (mData.contains(musicModel)) {
                mCurrent = mData.indexOf(musicModel);
            }
            return;
        }
        mCurrent = -1;
    }

    public void setOnMusicItemClickListener(OnMusicItemClickListener onMusicItemClickListener) {
        mOnMusicItemClickListener = onMusicItemClickListener;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(mInflater.inflate(R.layout.item_music, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
        holder.mTvNumber.setText(String.valueOf(position + 1));
        holder.mTvTitle.setText(mData.get(position).getTitle());
        holder.mTvArtist.setText(mData.get(position).getArtist());
        if (mCurrent == position) {
            holder.mVCurrent.setVisibility(View.VISIBLE);
            holder.mTvNumber.setTextColor(COLOR_LIGHT);
        } else {
            holder.mVCurrent.setVisibility(View.GONE);
            holder.mTvNumber.setTextColor(COLOR_NORMAL);
        }

        if (mOnMusicItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnMusicItemClickListener.onItemClick(holder.getAdapterPosition());
                }
            });
            holder.mIvMenu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnMusicItemClickListener.onMenuClick(holder.getAdapterPosition());
                }
            });
        }

        setSettingTheme(holder);
    }

    private void setSettingTheme(RecyclerViewHolder holder) {
        holder.mTvTitle.setTextColor(INSTANCE.getTEXT_COLOR());
        holder.mVCurrent.setBackgroundTintList(mThemeColorStateList);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * 更新列表的item
     */
    public void updateListItem() {
        int oldPosition = mCurrent;
        currentPositionOnList();
        notifyItemChanged(oldPosition);
        notifyItemChanged(mCurrent);
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView mTvNumber;
        TextView mTvTitle;
        TextView mTvArtist;
        ImageView mIvMenu;
        View mVCurrent;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            mTvNumber = (TextView) itemView.findViewById(R.id.id_item_tv_number);
            mTvTitle = (TextView) itemView.findViewById(R.id.id_item_tv_title);
            mTvArtist = (TextView) itemView.findViewById(R.id.id_item_tv_artist);
            mIvMenu = (ImageView) itemView.findViewById(R.id.id_item_iv_menu);
            mVCurrent = itemView.findViewById(R.id.id_item_v_current);
        }
    }
}
